package main

import (
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

type User struct {
	gorm.Model
	Name string
	Age int64
	Active bool
}


func main(){
	// 连接数据库
	db, err := gorm.Open("mysql", "root:root@(127.0.0.1)/db1?charset=utf8mb4&parseTime=True&loc=Local")
	if err != nil {
		panic(err)
	}

	defer db.Close()

	// 2.把模型与数据库中的表对应起来
	db.AutoMigrate(&User{})

	// 3.创建
	//u1 := User{Name: "xyz", Age: 19, Active: true}
	//db.Debug().Create(&u1)
	//u2 := User{Name: "abc", Age: 21, Active: false}
	//db.Create(&u2)

	// 删除
	//var u = User{}
	//
	////u.ID = 1
	//u.Name = "xyz"
	//db.Debug().Delete(&u)

	db.Where("name=?", "abc").Delete(User{})
}
